package utils
{
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	public class PolyGon
	{
		private var points:Array                     //存储所有的点
		private var bounds:Rectangle                 //多边形的范围
		public function PolyGon()
		{
			bounds=new Rectangle();
			points=new Array();
		}
		public function addPoint(inX:Number,inY:Number):void
		{
			var obj:Object={x:inX,y:inY};
			points.push(obj);
			updateBounds(inX,inY);
		}
		public function showBoundBox(target:Sprite):void
		{
			if(target!=null && points.length>0)
			{
				target.graphics.lineStyle(1,21312);
				target.graphics.lineTo(bounds.x,bounds.y);
				target.graphics.lineTo(bounds.x+bounds.width,bounds.y);
				target.graphics.lineTo(bounds.x+bounds.width,bounds.y+bounds.height);
				target.graphics.lineTo(bounds.x,bounds.y+bounds.height);
				target.graphics.lineTo(bounds.x,bounds.y);
				target.graphics.endFill();
			}
		}
		/**
		 * 显示多边形的所有边
		 */ 
		public function showPolyGon(target:Sprite):void
		{
			if(target!=null && points.length>0)
			{
				target.graphics.lineStyle(1,41312);
				for each(var obj:Object in points)
				{
					target.graphics.lineTo(obj.x,obj.y);
				}
				target.graphics.lineTo(points[0].x,points[0].y)
				target.graphics.endFill();
			}
		}
		// 更新边界的大小
	    private function updateBounds( x:int,y:int):void{
		    if (x < bounds.x) {
		        bounds.width = bounds.width + (bounds.x - x);
		        bounds.x = x;
		    }
		    else {
		        bounds.width = Math.max(bounds.width, x - bounds.x);
		    }
		 
		    if (y < bounds.y) {
		        bounds.height = bounds.height + (bounds.y - y);
		        bounds.y = y;
		    }
		    else {
		        bounds.height = Math.max(bounds.height, y - bounds.y);
		    }
	    } 
		/**
		 * 检测点是否在多边形中
		 */ 
		public function contains(x:Number,y:Number):Boolean
		{
			if(points.length==2)
			{
				return false;
			}
			var hits:int = 0;
            var lastx:int = points[points.length - 1].x;
            var lasty:int = points[points.length - 1].y;
            var curx:int, cury:int;
            for (var i:int = 0; i < points.length; lastx = curx, lasty = cury, i++) {
            	curx = points[i].x;
            	cury = points[i].y;
            	if (cury == lasty) {
            		continue;
            	}
            	var leftx:int;
                if (curx < lastx) {
                if (x >= lastx) {
                   continue;
                }
                leftx = curx;
                } else {
                if (x >= curx) {
                   continue;
                }
                leftx = lastx;
                }
		        var test1:Number, test2:Number;
		        if (cury < lasty) {
		        if (y < cury || y >= lasty) {
		            continue;
		        }
		        if (x < leftx) {
		            hits++;
		            continue;
		        }
		        test1 = x - curx;
		        test2 = y - cury;
		        } else {
		        if (y < lasty || y >= cury) {
		            continue;
	            }
	            if (x < leftx) {
		            hits++;
		            continue;
		        }
		        test1 = x - lastx;
		        test2 = y - lasty;
		        }
		        if (test1 < (test2 / (lasty - cury) * (lastx - curx))) {
		        hits++;
		        }
            }
            return ((hits & 1) != 0);
		}
	}
}